#ifndef MYSQLDB_H
#define MYSQLDB_H

#include <QObject>
#include <QSqlDatabase>
#include <QSqlQuery>
#include <QSqlError>
#include <QSqlRecord>
#include <QSqlField>
#include <QVariant>
#include <QJsonArray>
#include <QJsonObject>

struct DatabaseInfo {
    QString connName;
    QString server;
    QString user;
    QString passwd;
    QString dbName;
    int port;

    DatabaseInfo(const QString &_con = Q_NULLPTR,
                 const QString &_ser = Q_NULLPTR,
                 const QString &_usr = Q_NULLPTR,
                 const QString &_pwd = Q_NULLPTR,
                 const QString &name = Q_NULLPTR,
                 int _port = 3306)
        : connName(_con)
        , server(_ser)
        , user(_usr)
        , passwd(_pwd)
        , dbName(name)
        , port(_port) {}
};

class MysqlDB : public QObject
{
    Q_OBJECT
public:
    explicit MysqlDB(const QString &connectionName, QObject *parent = Q_NULLPTR, const DatabaseInfo &dbInfo = DatabaseInfo());
    virtual ~MysqlDB();

    void setConntionName(const QString &name);
    void setDatabaseInfo(const DatabaseInfo &info);
    void setHostName(const QString &host);
    void setUserName(const QString &user);
    void setPassword(const QString &pwd);
    void setDatabaseName(const QString &dbName);
    void setPort(int port);

    bool testConnect();

    QStringList databaseNames();
    QStringList tables();
    void sqlData(const QString &sql, QSqlRecord &record, QVector<QVariant> &data);

private:
    DatabaseInfo m_dbInfo;
    QString      m_connName;
};

#endif // MYSQLDB_H
